/**
 * Copyright (c) 2011-2015, James Zhan 詹波 (jfinal@126.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jfinal.ext.kit;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.jfinal.kit.StrKit;

/**
 * DateKit.
 */
public class DateKit {
    
	public static String dateFormat = "yyyy-MM-dd";
	public static String timeFormat = "yyyy-MM-dd HH:mm:ss";
	
	public static void setDateFromat(String dateFormat) {
		if (StrKit.isBlank(dateFormat))
			throw new IllegalArgumentException("dateFormat can not be blank.");
		DateKit.dateFormat = dateFormat;
	}
	
	public static void setTimeFromat(String timeFormat) {
		if (StrKit.isBlank(timeFormat))
			throw new IllegalArgumentException("timeFormat can not be blank.");
		DateKit.timeFormat = timeFormat;
	}
	
	public static Date toDate(String dateStr) {
		if (dateStr.contains("-")) {
			if (dateStr.contains(":")) {
				return toDate(dateStr, "yyyy-MM-dd HH:mm:ss");
			} else {
				return toDate(dateStr, "yyyy-MM-dd");
			}
		} else if (dateStr.contains(".")) {
			if (dateStr.contains(":")) {
				return toDate(dateStr, "yyyy.MM.dd HH:mm:ss");
			} else {
				return toDate(dateStr, "yyyy.MM.dd");
			}
		} else if (dateStr.contains("/")) {
			if (dateStr.contains(":")) {
				return toDate(dateStr, "yyyy/MM/dd HH:mm:ss");
			} else {
				return toDate(dateStr, "yyyy/MM/dd");
			}
		} else if (dateStr.length() == 8) {// yyyyMMdd格式
			return toDate(dateStr, "yyyyMMdd");
		} else if (dateStr.length() == 14) {// yyyyMMddHHmmss格式
			return toDate(dateStr, "yyyyMMddHHmmss");
		} else if (dateStr.length() == 12) {// yyyyMMddmm格式
			return toDate(dateStr, "yyyyMMddHHmm");
		} else {
			StringBuilder sb = new StringBuilder();
			sb.append("不支持日期字符串:");
			sb.append(dateStr);
			sb.append("对应的日期格式");
			throw new RuntimeException(sb.toString());
		}
	}

	public static Date toDate(String dateStr, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			return sdf.parse(dateStr);
		} catch (ParseException e) {
			StringBuilder sb = new StringBuilder();
			sb.append("日期字符串:");
			sb.append(dateStr);
			sb.append("与日期格式:");
			sb.append(format);
			sb.append("不匹配");
			throw new RuntimeException(sb.toString());
		}
	}

	public static String toStr(Date date) {
		return toStr(date, DateKit.dateFormat);
	}

	public static String toStr(java.sql.Date date) {
		return toStr(date, DateKit.dateFormat);
	}

	public static String toStr(Date date, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}

	public static String toStr(java.sql.Date date, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}
}
